#!/usr/bin/env bash
# es和kibana的搭建
echo "=============================="
echo " *********  docker   ********"
echo "=============================="
if !command -v docker >/dev/null 2>&1; then
    echo "not exists docker, Please install"
    return
fi

result=$(sudo cat /etc/group | grep docker)
if [[ "$result" != "" ]]
then
    echo "dockergroup exist..."
else
    sudo groupadd docker
fi

result=$(sudo cat /etc/group | grep $USER)
if [[ "$result" != "" ]]
then
    echo "user exist..."
else
    sudo usermod -aG docker $USER
fi

# # 安装es | 使用的es版本为7.14.0
echo "=============================="
echo " *********  es   *********"
echo "=============================="
docker pull elasticsearch:7.14.0
mkdir -p ~/opt/elasticsearch/config
mkdir -p ~/opt/elasticsearch/data
# sudo chmod -R 777 ~/opt/elasticsearch/
echo "http.host: 0.0.0.0" > ~/opt/elasticsearch/config/elasticsearch.yml
docker run --name elasticsearch -p 9200:9200  -p 9300:9300  -e "discovery.type=single-node" -e ES_JAVA_OPTS="-Xms84m -Xmx512m" -v ~/opt/elasticsearch/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml -v ~/opt/elasticsearch/data:/usr/share/elasticsearch/data -v ~/opt/elasticsearch/plugins:/usr/share/elasticsearch/plugins -d elasticsearch:7.14.0

# # 安装kibana
echo "=============================="
echo " *******  kibana   *********"
echo "=============================="
docker pull kibana:7.14.0
mkdir -p ~/opt/kibana    # 创建挂载配置目录
# sudo chmod 777 ~/opt/kibana   # 赋值读写执行权限
sudo echo "server.host: 0.0.0.0" > ~/opt/kibana/kibana.yml
read -p "请输入您的eth0的地址确保正确无误:::>>" DATA
echo "elasticsearch.hosts: http://$DATA:9200" >> ~/opt/kibana/kibana.yml # 0.0.0.0 表示你的ip
docker run --name kibana -v ~/opt/kibana/kibana.yml:/usr/share/kibana/config/kibana.yml -p 5601:5601 -d kibana:7.14.0

echo "=============================="
echo " *********  mysql   *********"
echo "=============================="
docker_env/mysql/mysql.sh
# read -p "按任意键继续！"

echo "=============================="
echo " ********   rabbitmq  ********"
echo "=============================="
docker_env/rabbitmq/rabbitmq.sh
read -p "按任意键继续！"

echo "=============================="
echo " ********   redis  ********"
echo "=============================="
docker_env/redis/redis.sh

echo "=============================="
echo " *****  esIndexClear  ******"
echo "=============================="
crontab -l > conf && echo "0 * * * * $PWD/es-index-clear.sh >> /tmp/tmp.txt" >> conf && crontab conf && rm -f conf

echo "=============================="
echo " *****  PM2GUI  ******"
echo "=============================="
echo "请确保已经安装了PM2"
sudo npm install pm2-gui -g
pm2 start $PWD/pm2GUI/pm2GUI.json

echo "=============================="
echo " *****  nginx  ******"
echo "=============================="
cd nginx
docker build -t nginx-apline:pub .
./nginx.sh

read -p "按任意键继续！"